define([] , function(){
	var ajaxExtend = {
		init 	: 	function( app ){
			app.service('$ajax' , function(){
				var loading ;
				var Promise = function(){
					this.callback = {} ;
					this.success = function( call ){
						if( typeof call == 'function' )
							this.callback['success'] = call ;
						return this ;
					}
					this.error = function( call ){
						if( typeof call != 'function' ){
							call = function(){
								alert('服务器请求出错,请重试!') ;
								
							}
						}
						this.callback['error'] = call ;
						return this ;
					}
					this.then = 	function( call ){
						if( typeof call == 'function' )
							this.callback['then'] = call ;
						return this ;
					}
					
					this.complete = 	function( call ){
						if( typeof call == 'function' )
							this.callback['complete'] = call ;
						return this ;
					}
				}
				this.get =	function( url , data , config ){
					return this.ajax( $.extend( true , config , {
						url 	: 	url ,
						type 	: 	'get' ,
						data 	: 	data
					} ) ) ;
				}
				this.post =	function( url , data , config ){
					return this.ajax( $.extend( true , config , {
						url 	: 	url ,
						type 	: 	'post' ,
						data 	: 	data ,
						contentType 	: 	'application/json;charset=UTF-8'
					} ) ) ;
				}
				this.put =	function( url , data , config ){
					return this.ajax( $.extend( true , config , {
						url 	: 	url ,
						type 	: 	'put' ,
						data 	: 	data ,
						contentType 	: 	'application/json;charset=UTF-8'
					} ) ) ;
				}
				this.remove =	function( url , data , config ){
					return this.ajax( $.extend( true , config , {
						url 	: 	url ,
						type 	: 	'delete' ,
						data 	: 	data ,
						contentType 	: 	'application/json;charset=UTF-8'
					} ) ) ;
				}
				
				this.ajax =	function( config ){
					var promise = new Promise() , options = {
						url 	: 	'' ,
						type 	: 	'get' ,
						dataType: 	'json' ,
						cache 	: 	false ,
						beforeSend 	: 	function(){
							
						},
						success : 	function( result , xmlHttp ){
							if( promise.callback.then )
								promise.callback.then.call( promise , 'success' , result , xmlHttp ) ;
							if( promise.callback.success )
								promise.callback.success.call( promise , result , xmlHttp  );
						},
						error 	:	function( xmlHttp , error ){
							if( xmlHttp.status == 404 ){
								
								alert('接口['+options['url']+']地址不存在!') ;
								return ;
							}
							if( xmlHttp.status == 401 ){
								alert('登陆超时,请重新登陆');
								setTimeout( function(){
									location.href='login.html' ;
								} , 2000 ) ;
								return ;
							}
							if( promise.callback.then )
								promise.callback.then.call( promise , 'error' , xmlHttp.status , xmlHttp ) ;
							if( promise.callback.error )
								promise.callback.error.call( promise , xmlHttp.status , xmlHttp  );
						},
						complete : 	function(){
							if( promise.callback.complete )
								promise.callback.complete.call( promise );
						}
					}
					$.extend( true , options , config ) ;
					$.ajax( options ) ;
					return promise ;
				}
			});
			return this ;
		}
	}
	
	return function( app ){
		ajaxExtend.init( app ) ;
	}
});